package com.itheima.controller;

import com.itheima.domain.PmsProductCategory;
import com.itheima.manager.PmsProductCategoryManager;
import com.itheima.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class PmsProductCategoryController {

    @Autowired
    PmsProductCategoryManager pmsProductCategoryManager;


    /**
     * 查询所有分类及其子分类
     * @return
     */
    @GetMapping("/productCategory/list/withChildren")
    public R findAll(){
        return pmsProductCategoryManager.findAll();
    }


    /**
     * 分页查询商品分类
     * @param parentId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/productCategory/list/{parentId}")
    public R findCategoryByPage(@PathVariable Integer parentId,
                           @RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "10") Integer pageSize){
        return pmsProductCategoryManager.findCategoryByPage(parentId, pageNum, pageSize);
    }

    /**
     * 添加商品分类
     * @param productCategoryParam
     * @return
     */
    @PostMapping("/productCategory/create")
    public R addCategory(@RequestBody PmsProductCategory productCategoryParam){
        return pmsProductCategoryManager.addCategory(productCategoryParam);
    }

    /**
     * 删除商品分类
     * @param id
     * @return
     */
    @PostMapping("/productCategory/delete/{id}")
    public R deleteCategory(@PathVariable Integer id){
        return pmsProductCategoryManager.deleteCategory(id);
    }

    /**
     * 修改导航栏显示状态
     * @param ids
     * @param navStatus
     * @return
     */
    @PostMapping("/productCategory/update/navStatus")
    public R updateNavStatus(@RequestParam List<Integer> ids,@RequestParam Integer navStatus){
        return pmsProductCategoryManager.updateNavStatus(ids,navStatus);
    }


    /**
     * 修改显示状态
     * @param ids
     * @param showStatus
     * @return
     */
    @PostMapping("/productCategory/update/showStatus")
    public R updateShowStatus(@RequestParam List<Integer> ids,@RequestParam Integer showStatus){
        return pmsProductCategoryManager.updateShowStatus(ids,showStatus);
    }


    /**
     * 修改商品分类
     * @param id
     * @param productCategoryParam
     * @return
     */
    @PostMapping("/productCategory/update/{id}")
    public R updateCategory(@PathVariable Integer id,@RequestBody PmsProductCategory productCategoryParam){
        //将id封装进要修改的对象
        productCategoryParam.setId(Long.valueOf(id));
        return pmsProductCategoryManager.updateCategory(productCategoryParam);
    }

    /**
     * 根据id获取商品分类
     * @param id
     * @return
     */
    @GetMapping("/productCategory/{id}")
    public R findCategoryById(@PathVariable Long id){
        return pmsProductCategoryManager.findCategoryById(id);
    }

}
